package com.rabbitmq.rabbitmqextensions.config;

import com.rabbitmq.rabbitmqextensions.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DLConfig {

    /**
     * 正常
     * @return
     */
    @Bean("norQueue")
    public Queue norQueue() {

        return QueueBuilder.durable(Constants.NOR_QUEUE)
//                .ttl(10000) //过期时间 10s
                .deadLetterExchange(Constants.DL_EXCHANGE) //绑定死信交换机
                .deadLetterRoutingKey(Constants.DL_ROUTINGKEY)
//                .maxLength(10L) //队列长度为 10
                .build();
    }
    @Bean("norExchange")
    public DirectExchange norExchange() {
        return ExchangeBuilder.directExchange(Constants.NOR_EXCHANGE).build();
    }
    @Bean("norBind")
    public Binding norBind(@Qualifier("norExchange") DirectExchange directExchange,
                           @Qualifier("norQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.NOR_ROUTINGKEY);
    }

    /**
     * 死信
     */
    @Bean("dlQueue")
    public Queue dlQueue() {
        return QueueBuilder.durable(Constants.DL_QUEUE).build();
    }
    @Bean("dlExchange")
    public DirectExchange dlExchange() {
        return ExchangeBuilder.directExchange(Constants.DL_EXCHANGE).build();
    }
    @Bean("dlBind")
    public Binding dlBind(@Qualifier("dlExchange") DirectExchange directExchange,
                           @Qualifier("dlQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.DL_ROUTINGKEY);
    }
}
